package com.xj.zhihudemo.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.mode.LastNewsInfo;
import com.xj.zhihudemo.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xujing on 16/9/7.
 */
public class MainNewsAdapter extends RecyclerView.Adapter<RecyclerView
        .ViewHolder> {

    private static final int NORMA_ITEM = 0;
    private static final int GROUP_ITEM = 1;


    private Context mContext;
    private LayoutInflater mInflater;
    private LastNewsInfo mLastNewsInfo;
    private List<LastNewsInfo.StoriesEntity> mLastNewsData;
    private OnItemClickHelper mOnItemClickHelper;

    private ArrayList<Integer> idList;

    public MainNewsAdapter(Context context) {
        this.mContext = context;
        mLastNewsInfo = new LastNewsInfo();
        mInflater = LayoutInflater.from(mContext);
        mLastNewsData = new ArrayList<LastNewsInfo.StoriesEntity>();
        idList = new ArrayList<>();

    }

    public void setOnItemClick(OnItemClickHelper onItemClick) {
        this.mOnItemClickHelper = onItemClick;
    }

    public void addData(LastNewsInfo mData) {

        mLastNewsInfo = mData;
        mLastNewsData.clear();
        mLastNewsData.addAll(mData.getStories());
        if (mLastNewsData != null) {
            idList.clear();
            for (LastNewsInfo.StoriesEntity storiesEntity : mLastNewsData) {
                idList.add(storiesEntity.getId());
            }
        }
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mLastNewsData != null) {
            mLastNewsData.clear();
            notifyDataSetChanged();
        }
    }

    public ArrayList<Integer> getIdList() {
        return idList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int
            viewType) {
        if (viewType == GROUP_ITEM) {
            View view = mInflater.inflate(R.layout.item_news_group, parent,
                    false);
            return new GroupItemHolder(view);
        } else {
            return new NormalItemHolder(mInflater.inflate(R.layout
                    .item_news_image, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int
            position) {
        final LastNewsInfo.StoriesEntity storiesEntity = mLastNewsData.get
                (position);
        if (null == storiesEntity)
            return;
        if (holder instanceof GroupItemHolder) {
            String date = DateUtils.FriendlyDate(mLastNewsInfo.getDate());
            ((GroupItemHolder) holder).mTvGroupTime.setText(date);
            ((GroupItemHolder) holder).mTvNewsText.setText(storiesEntity.getTitle());
            if (storiesEntity.getImages() != null) {
                ((GroupItemHolder) holder).mIvNewsImage.setVisibility(View.VISIBLE);
                ((GroupItemHolder) holder).mIvNewsImage
                        .setImageURI(storiesEntity.getImages().get(0));
            }
        } else if (holder instanceof NormalItemHolder) {
            ((NormalItemHolder) holder).mTvNewsText.setText(storiesEntity.getTitle());
            if (storiesEntity.getImages() != null) {
                ((NormalItemHolder) holder).mIvNewsImage.setVisibility(View.VISIBLE);
                ((NormalItemHolder) holder).mIvNewsImage
                        .setImageURI(storiesEntity.getImages().get(0));
            }
        }

        if (mOnItemClickHelper != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener
                    () {
                @Override
                public void onClick(View v) {
                    mOnItemClickHelper.onItemClick(v, position, storiesEntity, idList);
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return GROUP_ITEM;
        return NORMA_ITEM;
//        String currentDate = mLastNewsInfo.getDate().get(position)
// .getPublishDate();
//        int prevIndex = position - 1;
//        boolean isDifferent = !mDataList.get(prevIndex).getPublishDate()
// .equals(currentDate);
//        return isDifferent ? GROUP_ITEM : NORMAL_ITEM;

    }

    @Override
    public int getItemCount() {
        return mLastNewsData.size();
    }

    //Item点击回调
    public interface OnItemClickHelper {
        void onItemClick(View view, int position, LastNewsInfo.StoriesEntity
                data, ArrayList<Integer> idList);

        void onItemLongClick(View view, int position, LastNewsInfo
                .StoriesEntity data);
    }

    public class NormalItemHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView mIvNewsImage;

        TextView mTvNewsText;

        public NormalItemHolder(final View itemView) {
            super(itemView);
            mIvNewsImage = (SimpleDraweeView) itemView.findViewById(R.id
                    .iv_news_image);
            mTvNewsText = (TextView) itemView.findViewById(R.id.tv_news_text);

        }
    }

    public class GroupItemHolder extends RecyclerView.ViewHolder {
        TextView mTvGroupTime;
        SimpleDraweeView mIvNewsImage;
        TextView mTvNewsText;

        public GroupItemHolder(View itemView) {
            super(itemView);
            mTvGroupTime = (TextView) itemView.findViewById(R.id
                    .tv_group_time);
            mIvNewsImage = (SimpleDraweeView) itemView.findViewById(R.id
                    .iv_news_image);
            mTvNewsText = (TextView) itemView.findViewById(R.id.tv_news_text);
        }
    }


}

